package com.mbti.youmore.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mbti.youmore.common.exception.*;
import com.mbti.youmore.entity.*;
import com.mbti.youmore.mapper.*;
import com.mbti.youmore.service.LikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Guqier
 * @description 针对表【t_like】的数据库操作Service实现
 * @createDate 2024-05-08 21:19:11
 */
@Service
public class LikeServiceImpl extends ServiceImpl<LikeMapper, Like>
        implements LikeService {


    @Autowired
    private LikeMapper likemapper;
    @Autowired
    private UserMapper usermapper;
    @Autowired
    private PostMapper postmapper;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private CommentLikeMapper commentLikeMapper;

    @Override
    @Transactional//开启事务
    public boolean giveLike(Integer postId, Integer userId) {
        Post post = postmapper.selectById(postId);
        User user = usermapper.selectById(userId);
        if (post == null) {
            throw new PostNotExistException("帖子不存在");
        }

        if (user == null) {
            throw new UserNotExistsException("用户不存在");
        }

        if (likemapper.selectCount(new LambdaQueryWrapper<Like>().eq(Like::getPostId, postId).eq(Like::getUserId, userId)) != 0) {
            throw new LikeExistException("该用户已经点赞过了");
        }
        Like like = new Like();
        like.setUserId(userId);
        like.setPostId(postId);
        like.setLikeDate(new Timestamp(new Date().getTime()));
        int res = likemapper.insert(like);
        post.setLikeNum(post.getLikeNum() + 1);
        int res2 = postmapper.updateById(post);
        return res == 1 && res2 == 1;
    }

    @Override
    public List<Integer> getLikeUserIdByPostId(Integer postId) {
        Post post = postmapper.selectById(postId);
        if (post == null) {
            throw new PostNotExistException("帖子不存在");
        }
        List<Integer> userIds = null;
        userIds = likemapper.getLikeUserIdByPostId(postId);
        return userIds;
    }

    @Override
    public boolean deleteLike(Integer postId, Integer userId) {
        Post post = postmapper.selectById(postId);
        User user = usermapper.selectById(userId);
        if (post == null) {
            throw new PostNotExistException("帖子不存在");
        }

        if (user == null) {
            throw new UserNotExistsException("用户不存在");
        }
        if (likemapper.selectCount(new LambdaQueryWrapper<Like>().eq(Like::getPostId, postId).eq(Like::getUserId, userId)) == 0) {
            throw new LikeExistException("该用户还没点赞");
        }
        int res = likemapper.delete(new LambdaQueryWrapper<Like>().eq(Like::getPostId, postId).eq(Like::getUserId, userId));
        post.setLikeNum(post.getLikeNum() - 1);
        int res2 = postmapper.updateById(post);
        return res == 1 && res2 == 1;
    }

    @Override
    public boolean giveCommentLike(Integer userId, Integer commentId) {
        if (commentId == null) return false;
        Comment comment = commentMapper.selectById(commentId);
        if (comment == null) throw new CommentNotExistException();
        if (commentLikeMapper.selectCount(new LambdaQueryWrapper<CommentLike>().eq(CommentLike::getCommentId, commentId).eq(CommentLike::getUserId, userId)) != 0)
            throw new LikeExistException();

        CommentLike commentLike = new CommentLike();
        commentLike.setUserId(userId);
        commentLike.setCommentId(commentId);
        commentLike.setTime(new Timestamp(new Date().getTime()));
        return commentLikeMapper.insert(commentLike) == 1;
    }

    @Override
    public boolean deleteCommentLike(Integer userId, Integer commentId) {
        if (commentId == null) return false;
        Comment comment = commentMapper.selectById(commentId);
        if (comment == null) throw new CommentNotExistException();
        if (commentLikeMapper.selectCount(new LambdaQueryWrapper<CommentLike>().eq(CommentLike::getCommentId, commentId).eq(CommentLike::getUserId, userId)) == 0)
            throw new LikeNotExistException();
        return commentLikeMapper.delete(new LambdaQueryWrapper<CommentLike>().eq(CommentLike::getCommentId, commentId).eq(CommentLike::getUserId, userId)) == 1;
    }

}




